package org.SSV.NLES.client;

import com.bouwkamp.gwt.user.client.ui.RoundedPanel;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class LabelTextBoxPanel extends Composite implements FocusListener, ClickListener {
	private String	dieLangeDesTextKastes;

	private Label	label		= new Label("");

	private TextBox	textbox		= new TextBox();

	private Label	topLabel	= new Label("");

	public LabelTextBoxPanel(String lab) {
		LabelTextBoxPanel dock = new LabelTextBoxPanel(lab, "", "OE", "5cm", "RoundedPanel");
		initWidget(dock);
	}

	/**
	 * @param lab
	 * @param wohin
	 *            N,S,OE,W,
	 * @param eLange
	 *            die Lange des Kastes
	 */
	public LabelTextBoxPanel(String lab, String wohin, String eLange) {
		LabelTextBoxPanel dock = new LabelTextBoxPanel(lab, "", wohin, eLange);
		initWidget(dock);
	}

	/**
	 * @param lab
	 * @param rTexte
	 * @param wohin
	 *            N,S,OE,W,
	 * @param eLange
	 *            die Lange des Kastes
	 * @param rTopf
	 *            RoundedPanel,...
	 */
	public LabelTextBoxPanel(String lab, String rTexte, String wohin, String eLange) {
		LabelTextBoxPanel dock = new LabelTextBoxPanel(lab, "", wohin, eLange, "");
		initWidget(dock);
	}

	/**
	 * @param lab
	 * @param rTexte
	 * @param wohin
	 *            N,S,OE,W,
	 * @param eLange
	 *            die Lange des Kastes
	 * @param rTopf
	 *            RoundedPanel,...
	 */
	public LabelTextBoxPanel(String lab, String rTexte, String wohin, String eLange, String rTopf) {
		dieLangeDesTextKastes = eLange;

		Panel dock;

		if (wohin.equals("N") || wohin.equals("S")) {
			dock = new VerticalPanel();
		} else {
			dock = new HorizontalPanel();
		}

		if (wohin.equals("N") || wohin.equals("W")) {
			dock.add(getTopLabel());
			dock.add(getTextbox());
			dock.add(getLabel());
		} else {
			dock.add(getLabel());
			dock.add(getTextbox());
			dock.add(getTopLabel());
		}

		getLabel().setText(lab);
		getTextbox().setText(rTexte);
		getTextbox().setWidth(dieLangeDesTextKastes);
		getTopLabel().setVisible(false);
		getTextbox().addFocusListener(this);
		getLabel().addClickListener(this);
		getTopLabel().addClickListener(this);

		if (rTopf.equals("")) {
			initWidget(dock);
		} else {
			if (rTopf.equals("RoundedPanel")) {
				RoundedPanel rp = new RoundedPanel();
				rp.add(dock);
				initWidget(rp);
			}
		}
	}

	/**
	 * @return Returns the label.
	 */
	public Label getLabel() {
		return label;
	}

	/**
	 * @return Returns the textbox.
	 */
	public TextBox getTextbox() {
		return textbox;
	}

	/**
	 * @return Returns the topLabel.
	 */
	private Label getTopLabel() {
		return topLabel;
	}

	public void onClick(Widget sender) {
		if (sender == getTopLabel()) {
			if (getTextbox().isVisible() == false) {
				getTopLabel().setVisible(false);
				getTextbox().setVisible(true);
				getTextbox().setFocus(true);
				getTextbox().selectAll();
			}
		}

		if (sender == getLabel()) {
			onClick(getTopLabel());
		}
	}

	public void onFocus(Widget sender) {
// TODO: Document empty method
	}

	public void onLostFocus(Widget sender) {
		if (sender == getTextbox()) {
			getTextbox().setVisible(false);
			getTopLabel().setVisible(true);
			getTopLabel().setWidth(
					String.valueOf(((getTextbox().getText().length() * 1) / 4) + 0) + "cm");
			getTopLabel().setText(getTextbox().getText());
		}
	}
}
